package com.likou;

/**
 * Author: 陈桂明
 * Date: 2025/8/30 23:44
 * Description: q41
 */
public class q41 {

    public static void main(String[] args) {

    }

    public int firstMissingPositive(int[] nums) {
        int len = nums.length;

        for (int i = 0; i < len; i++) {
            while (nums[i] >= 1 && nums[i] <= len && nums[nums[i] - 1] != nums[i]) {
                int target = nums[i] - 1;
                int temp = nums[i];
                nums[i] = nums[target];
                nums[target] = temp;
            }
        }

        for (int i = 0; i < len; i++) {
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }


        return len + 1;
    }
}
